require "lunit"
require "argot"

lunit.setprivfenv()
lunit.import "assertions"


-- little helper

function makeBoundFunction(obj, method)
	func = function(...)
	            -- table.foreach(arg, print)
				return method(obj, unpack(arg))
			end
	return func
end



-- actual testcase
testcase = lunit.TestCase("TypeElement")


function testcase.setup(self)
	self.lib = argot.TypeLibrary()
    assert_equal( argot.type_core_init( self.lib ), argot.TYPE_OK )
end

function testcase.teardown(self)
	self.lib = nil
end



function testcase.testbind(self)
    name = argot.TypeName('test')

	e = argot.TypeElement( makeBoundFunction(self, self.bind) )
    print('Registering')
    self.lib:Register( name, e )
    print('Done')
end

function testcase.bind(self, element, definition)
	print('I was called! Hooray!')

    rc = element.object:bind( definition.library, argot.meta_basic_name )
	if rc ~= argot.TYPE_OK then return rc end

    element:Bind( definition )
    rc = element.object:bind( definition.library, argot.meta_basic_name )
    if rc ~= argot.TYPE_OK then return rc end

    print('Exiting bind')

    return argot.TYPE_OK

end


lunit.run()